package com.waimai.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.waimai.api.model.ResponseModel;
import com.waimai.db.entity.Comment;
import com.waimai.db.entity.Food;
import com.waimai.db.entity.Order;
import com.waimai.db.entity.OrderStatus;
import com.waimai.db.entity.Restaurant;
import com.waimai.db.entity.User;
import com.waimai.db.service.RestaurantService;
import com.waimai.util.SystemUtil;

@Controller
public class RestaurantController {

	@Autowired
	public RestaurantService restaurantService;

	@ResponseBody
	@RequestMapping(value="/restaurants", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<List<Restaurant>> restuarants(@RequestBody String keyWords, HttpServletRequest request){
		List<Restaurant> data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = restaurantService.findBykeyWords(keyWords);
			for(int i=0;i<data.size();i++){
				String photoName = data.get(i).getPhoto();
				if(photoName != null && !"".equals(photoName)){
					data.get(i).setPhoto(SystemUtil.getPhotoUrl(request, photoName));
				}
			}
			if(data.size() <= 0){
				statusCode = ResponseModel.STATUS_GET_DATA_NONE;
			}else{
				statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
			}

		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<List<Restaurant>>(data, statusCode);
	}


	@ResponseBody
	@RequestMapping(value="/restaurant/{restaurantId}", method=RequestMethod.GET)
	public ResponseModel<Restaurant> restuarant(@PathVariable("restaurantId") Integer restaurantId){
		Restaurant data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = restaurantService.findById(restaurantId);
			if(data == null){
				statusCode = ResponseModel.STATUS_GET_DATA_NONE;
			}else{
				statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<Restaurant>(data, statusCode);
	}

	@ResponseBody
	@RequestMapping(value="/restaurant/{restaurantId}/foods", method=RequestMethod.GET)
	public ResponseModel<List<Food>> getRestaurantFoods(@PathVariable("restaurantId") Integer restaurantId,
			HttpServletRequest request){
		List<Food> data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = restaurantService.getFoods(restaurantId);
			for(int i=0;i<data.size();i++){
				String photoName = data.get(i).getPhoto();
				if(photoName != null && !"".equals(photoName)){
					data.get(i).setPhoto(SystemUtil.getPhotoUrl(request, photoName));
				}
			}
			if(data.size() <= 0){
				statusCode = ResponseModel.STATUS_GET_DATA_NONE;
			}else{
				statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
			}

		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<List<Food>>(data, statusCode);
	}

	@ResponseBody
	@RequestMapping(value="/restaurant/{restaurantId}/check", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<Restaurant> checkRestaurant(@PathVariable("restaurantId") Integer restaurantId,
			@RequestBody Restaurant restaurant){
		Restaurant data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = restaurantService.findById(restaurant.getRestaurantId());
			if(data == null){
				statusCode = ResponseModel.STATUS_CHECK_NOT_EXTIST;
			}else{
				if(data.getLastUpdateTime().getTime() != restaurant.getLastUpdateTime().getTime()){
					statusCode = ResponseModel.STATUS_CHECK_NOT_PASSED;
				}else{
					statusCode = ResponseModel.STATUS_CHECK_PASSED;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<Restaurant>(data, statusCode);
	}

	@ResponseBody
	@RequestMapping(value="/restaurant/{restaurantId}/order", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<Order> addOrder(@PathVariable("restaurantId") Integer restaurantId,
			@RequestBody Order order){
		Order data = null;
		Restaurant temp = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			temp = restaurantService.findById(order.getRestaurant().getRestaurantId());
			if(temp == null){
				statusCode = ResponseModel.STATUS_CHECK_NOT_EXTIST;
				return new ResponseModel<Order>(null, statusCode);
			}else{
				if(temp.getLastUpdateTime().getTime() != order.getRestaurant().getLastUpdateTime().getTime()){
					statusCode = ResponseModel.STATUS_CHECK_NOT_PASSED;
					return new ResponseModel<Order>(order, statusCode);
				}else{
					statusCode = ResponseModel.STATUS_CHECK_PASSED;
				}
			}
			if(statusCode == ResponseModel.STATUS_CHECK_PASSED){
				data = restaurantService.addOrder(order);
				if(data == null){
					statusCode = ResponseModel.STATUS_REQUEST_FAILED;
					data = order;
				}else{
					statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<Order>(data, statusCode);
	}

	@ResponseBody
	@RequestMapping(value="/restaurant/{restaurantId}/order/{orderId}", method=RequestMethod.PUT, consumes="application/json")
	public ResponseModel<OrderStatus> updateOrderStatus(@PathVariable("restaurantId") Integer restaurantId,
			@PathVariable("orderId") Integer orderId,
			@RequestBody OrderStatus orderStatus){
		OrderStatus data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = restaurantService.updateOrderSatus(orderId, orderStatus);
			if(data == null){
				statusCode = ResponseModel.STATUS_REQUEST_FAILED;
				data = orderStatus;
			}else{
				statusCode = ResponseModel.STATUS_REQUEST_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<OrderStatus>(data, statusCode);
	}
	
	@ResponseBody
	@RequestMapping(value="/restaurant/{restaurantId}/comments", method=RequestMethod.POST, consumes="application/json")
	public ResponseModel<List<Comment>> comments(@PathVariable("restaurantId") Integer restaurantId,
			@RequestBody int commentLevel){
		List<Comment> data = null;
		int statusCode = ResponseModel.STATUS_SERVER_ERROR;
		try {
			data = restaurantService.getComments(restaurantId, (Integer)commentLevel);
			if(data != null){
				if(data.size() == 0){
					statusCode = ResponseModel.STATUS_GET_DATA_NONE;
				}else{
					statusCode = ResponseModel.STATUS_GET_DATA_SUCCESS;
				}
			}else{
				statusCode = ResponseModel.STATUS_SERVER_ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = ResponseModel.STATUS_SERVER_ERROR;
			data = null;
		}
		return new ResponseModel<List<Comment>>(data, statusCode);
	}
}
